package com.neuedu.controller;

import com.alibaba.fastjson2.JSONObject;
import com.neuedu.base.R;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.XiaoQuService;
import com.neuedu.service.impl.XiaoQuServiceImpl;
import com.neuedu.vo.PageVO;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class XiaoQuController {

    private XiaoQuService xiaoQuService=new XiaoQuServiceImpl();


    public R page(JSONObject jsonObject){
        //String minDate,String maxDate,String likeName
        String minDate = jsonObject.getString("minDate");
        String maxDate = jsonObject.getString("maxDate");
        String likeName = jsonObject.getString("likeName");
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        PageVO<XiaoQu> pageVO = xiaoQuService.selectPage(minDate, maxDate, likeName, pageNo, pageSize);

        return R.ok("分页数据查询成功").setData(pageVO);
    }

    public static final String FILE_PATH="E:\\java07";


    public R query(Map<String,String[]> map){
        Integer id = Integer.parseInt(map.get("id")[0]);

        XiaoQu xiaoQu = xiaoQuService.queryXiaoQu(id);

        return R.ok("详细信息查询成功").setData(xiaoQu);
    }




    public R del(Map<String,String[]> map){
        String [] idsStr = map.get("id");
        Integer [] ids=new Integer[idsStr.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i]=Integer.parseInt(idsStr[i]);
        }
        //调用service层，把ids传入，service帮我删去数据。
        int result = xiaoQuService.delXiaoQu(ids);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    public R save(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("id");
        Integer userid = jsonObject.getInteger("userid");
        String mingcheng = jsonObject.getString("mingcheng");
        String dizhi = jsonObject.getString("dizhi");
        String mianji = jsonObject.getString("mianji");
        String zongdongshu = jsonObject.getString("zongdongshu");
        String zonghushu = jsonObject.getString("zonghushu");
        String lvhualv = jsonObject.getString("lvhualv");
        String kaifashang = jsonObject.getString("kaifashang");
        String wuye = jsonObject.getString("wuye");
        String description = jsonObject.getString("description");
        String base64=jsonObject.getString("base64");
        Integer state=jsonObject.getInteger("state");
        XiaoQu xiaoQu=new XiaoQu();
        if(base64!=null){
            byte[] bytes = Base64.decodeBase64(base64);
            String fileName=UUID.randomUUID().toString().replace("-","").toUpperCase()+".jpg";
            File file=new File(FILE_PATH+ File.separator+fileName);
            FileOutputStream fos=null;
            try {
                fos=new FileOutputStream(file);
                fos.write(bytes);
                fos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(fos!=null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String icon=fileName;
            xiaoQu.setIcon(icon);
        }else{
            xiaoQu.setIcon(null);
        }
        xiaoQu.setId(id);
        xiaoQu.setUserid(userid);
        xiaoQu.setMingcheng(mingcheng);
        xiaoQu.setDizhi(dizhi);
        xiaoQu.setMianji(mianji);
        xiaoQu.setZongdongshu(zongdongshu);
        xiaoQu.setZonghushu(zonghushu);
        xiaoQu.setLvhualv(lvhualv);
        xiaoQu.setKaifashang(kaifashang);
        xiaoQu.setWuye(wuye);
        xiaoQu.setDescription(description);
        xiaoQu.setState(state);
        //service方法的保存
        int result = xiaoQuService.saveXiaoQu(xiaoQu);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }

    }
    public R add(JSONObject jsonObject){
        Integer userid = jsonObject.getInteger("userid");
        String mingcheng = jsonObject.getString("mingcheng");
        String dizhi = jsonObject.getString("dizhi");
        String mianji = jsonObject.getString("mianji");
        String zongdongshu = jsonObject.getString("zongdongshu");
        String zonghushu = jsonObject.getString("zonghushu");
        String lvhualv = jsonObject.getString("lvhualv");
        String kaifashang = jsonObject.getString("kaifashang");
        String wuye = jsonObject.getString("wuye");
        String description = jsonObject.getString("description");
        String base64=jsonObject.getString("base64");
        Integer state=jsonObject.getInteger("state");

        byte[] bytes = Base64.decodeBase64(base64);
        String fileName=UUID.randomUUID().toString().replace("-","").toUpperCase()+".jpg";
        File file=new File(FILE_PATH+ File.separator+fileName);
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(file);
            fos.write(bytes);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String icon=fileName;

        XiaoQu xiaoQu=new XiaoQu();
        xiaoQu.setUserid(userid);
        xiaoQu.setMingcheng(mingcheng);
        xiaoQu.setDizhi(dizhi);
        xiaoQu.setMianji(mianji);
        xiaoQu.setZongdongshu(zongdongshu);
        xiaoQu.setZonghushu(zonghushu);
        xiaoQu.setLvhualv(lvhualv);
        xiaoQu.setKaifashang(kaifashang);
        xiaoQu.setWuye(wuye);
        xiaoQu.setDescription(description);
        xiaoQu.setIcon(icon);
        xiaoQu.setState(state);
        int result = xiaoQuService.addXiaoQu(xiaoQu);
        if(result>0){
            return R.ok("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    public R showAll(Map<String,String[]> map){
        List<XiaoQu> xiaoQus = xiaoQuService.showAll();
        return R.ok("查询成功").setData(xiaoQus);
    }

}
